﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.Common
{
    public class MD5Encrypt
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string Encrypt(string source)
        {
            if (string.IsNullOrEmpty(source)) return string.Empty;
            HashAlgorithm provider = CryptoConfig.CreateFromName("MD5") as HashAlgorithm;
            byte[] bytes = Encoding.UTF8.GetBytes(source);//这里需要区别编码的
            byte[] hashValue = provider.ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 16; i++)
            {
                sb.Append(hashValue[i].ToString("x2"));
            }
            return sb.ToString();
        }
        /// <summary>
        /// 修改密码，MD5加密是不可逆的，所以要修改的话就要覆盖原密码
        /// </summary>
        /// <param name="sourceData"></param>
        /// <returns></returns>
        public static string Md5Encrypt(string sourceData)
        {
            string str3;
            Encoding encoding = new UTF8Encoding();
            byte[] bytes = encoding.GetBytes("12345678");
            byte[] rgbIV = new byte[] { 1, 2, 3, 4, 5, 6, 8, 7 };
            string s = sourceData;
            try
            {
                ICryptoTransform transform = new DESCryptoServiceProvider().CreateEncryptor(bytes, rgbIV);
                byte[] inputBuffer = encoding.GetBytes(s);
                str3 = Convert.ToBase64String(transform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
            }
            catch
            {
                throw;
            }
            return str3;
        }
    } }
